/**
 *    @file       threadmutex.h
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       12/12/2012 11:24:51 AM
 *
 *    @author     Theophilus, wangtf418@gmail.com
 */
#ifndef _BF_THREADMUTEX_H_
#define _BF_THREADMUTEX_H_
#include <pthread.h>

class threadmutex
{
public:
	threadmutex()
	{
		pthread_mutex_init(&m_mutex, NULL);
	}

	~threadmutex()
	{
		pthread_mutex_destroy(&m_mutex);
	}

public:
	inline void lock()
	{
		pthread_mutex_lock(&m_mutex);
	}

	inline void unlock()
	{
		pthread_mutex_unlock(&m_mutex);
	}

	inline int trylock()
	{
		return pthread_mutex_trylock(&m_mutex);
	}

private:
	pthread_mutex_t m_mutex;
};

class lockhelper
{
public:
	lockhelper(threadmutex *lock)
		: m_lock(lock)
	{
		m_lock->lock();
	}

	~lockhelper()
	{
		m_lock->unlock();
	}

private:
	threadmutex *m_lock;
};

#endif

